import a from 'axios'
import ls from 'store2'
// let access=ls.get('userData') || ''
// let accesstoken;
// access===''?accesstoken='':accesstoken=access.accessToken
const axios = a.create({	
	baseURL: '/shengxueApi' ,
	timeout: 30000
})

const getHeader = () => {
	// let headers = {'content-type':'application/json','Authorization': 'Bearer'}	
	let headers = {
		'content-type': 'application/json',
		'Access-Control-Allow-Origin': '*',
		'Api-Version': '2.0.1',	
		'accessToken':ls.get('tokens') 
	
	}

	return headers
}
// 返回状态判断
axios.interceptors.response.use((res) => {
	if (res.status === 200) {
		res.data.success = res.data.code === 0
		return res.data
	} else {
		return Promise.reject(res)
	}
}, (error) => {
	if (error && error.response && error.response.status === 401) {
		// window.$nuxt.$store.commit('setLoginDisplay', true)
		Message({
			title: '提示',
			message: '登录已失效，请重新登录',
			type: 'error',
			center: true,
			showClose: false,
			duration: 2000
		})
		window.$nuxt.$store.commit('clearToken')
		window.$nuxt.$router.replace('/login')
	}
	return Promise.reject(error)
})

let loginLoading = false;
let loginPromise;
let methods = {
	put_http(url, params) {
		return axios({
			method: 'PUT',
			url,
			data: params,
			headers: getHeader()
		})
	},
	delete_http(url, params) {
		return axios({
			method: 'DELETE',
			url,
			data: params,
			headers: getHeader()
		})
	},
	post_http(url, params) {
		return axios({
			method: 'POST',
			url,
			data: params,
			headers: getHeader()
		})
	},
	postForm_http(url, params) {
		let headers = getHeader()
		headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
		return axios({
			method: 'POST',
			url,
			data: qs.stringify(params),
			headers
		})
	},
	get_http(url, params) {
		return axios({
			method: 'GET',
			url,
			params,
			headers: getHeader()
		})
	},
	put(url, params, requireLogin) {
		if (requireLogin && loginLoading) {
			return loginPromise.then(() => {
				return this.put_http(url, params)
			})
		} else {
			return this.put_http(url, params)
		}
	},
	get(url, params, requireLogin) {
		if (requireLogin && loginLoading) {
			return loginPromise.then(() => {
				return this.get_http(url, params)
			})
		} else {
			return this.get_http(url, params)
		}
	},
	post(url, params, requireLogin) {
		if (requireLogin && loginLoading) {
			return loginPromise.then(() => {
				return this.post_http(url, params)
			})
		} else {
			return this.post_http(url, params)
		}
	},
	login(code) {
		loginLoading = true;
		loginPromise = this.post('/login', {
			"code": code,
		}).then(res => {
			loginLoading = false;
			const {
				token,
				openId,
				sign
			} = res.data;
			// console.log("res登录",res)
			ls.set('token', token);
			ls.set('sign', sign);
			ls.set('openId', openId);
			// this.userInfo();
		}).catch(err => {
			loginLoading = false;
			ls.set('token', null);
		});
	},
	//   userInfo(){
	//     this.get('/room',null,true).then(res=>{
	//       if(res.code === 0){
	//         window.$nuxt.$store.commit('room',res.data);
	//         if(res.data && res.data.length>=0){
	//           window.$nuxt.$router.replace('/home')
	//         }else{
	//           window.$nuxt.$router.replace('/homeVisitor')
	//         }
	//       }
	//     })
	//     this.get('/car',null,true).then(res=>{
	//       if(res.code === 0){
	//         window.$nuxt.$store.commit('car',res.data);
	//       }
	//     })
	//     this.get('/other/notice',null,true).then(res=>{
	//       if(res.code === 0){
	//         window.$nuxt.$store.commit('notice',res.data);
	//       }
	//     })
	//   },
}
export default methods
